package com.ibatis.dao;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.orm.ibatis.SqlMapClientCallback;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;




import com.ibatis.sqlmap.client.SqlMapExecutor;






public class BaseDAOImp extends SqlMapClientDaoSupport  implements IBaseDAO {




 public int count(String strSql)
 {
  Integer result=0;
  try {
   result=(Integer)getSqlMapClient().queryForObject(strSql);
   //return result;
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return result;
 }




 public <T> void batchDelete(final String sqlStr, final List<T> list) {
  // TODO Auto-generated method stub

        this.getSqlMapClientTemplate().execute( new SqlMapClientCallback() {

            public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {

                executor.startBatch();

                for (T member : list) {

                    executor.delete(sqlStr, member);

                }

                executor.executeBatch();

                return null;

            }

        });
 }





 public <T> void batchInsert(final String sqlStr,final List<T> list) {
  // TODO Auto-generated method stub
  SqlMapClientCallback callback = new SqlMapClientCallback() {

            public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {

                executor.startBatch();

                for (T member : list) {

                    executor.insert(sqlStr, member);

                }

                executor.executeBatch();

                return null;

            }

        };

        this.getSqlMapClientTemplate().execute(callback);
 }





 public <T> void batchUpdate(final String sqlStr, final List<T> list) {
  // TODO Auto-generated method stub
   SqlMapClientCallback callback = new SqlMapClientCallback() {

             public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {

                 executor.startBatch();

                 for (T member : list) {

                     executor.update(sqlStr, member);

                 }

                 executor.executeBatch();

                 return null;

             }

         };

         this.getSqlMapClientTemplate().execute(callback);
 }










 public <T> int delete(String sqlStr, T primaryKey) {
  // TODO Auto-generated method stub


   int rows=-1;
  try {
   rows = getSqlMapClient().delete(

      sqlStr, primaryKey);
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

     return rows;
 }





 @SuppressWarnings("unchecked")
 public <T extends Object, PK> T getOne(String sqlStr, PK primaryKey) {
  // TODO Auto-generated method stub
  T reuslt=null;
  try {
   reuslt=(T) getSqlMapClient().queryForObject(sqlStr, primaryKey);

  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return  reuslt;
 }





 public <T> T insert(String sqlStr, T entity) {
  // TODO Auto-generated method stub
  T result=null;
  try {
   result=(T) getSqlMapClient().insert(sqlStr, entity);

  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return  result;
 }





 public <T> List<T> selectAll(String sqlStr) {
  // TODO Auto-generated method stub
  List<T> list=new ArrayList<T>();
  try {
   list=  getSqlMapClient().queryForList(sqlStr);
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return list;
 }





 public <T> int update(String sqlStr, T entity) {
  // TODO Auto-generated method stub
   int rows=-1;
  try {
   rows = getSqlMapClient().update(

      sqlStr, entity);
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

         return rows;
 }



}


